﻿using System.Collections;
using System.Windows.Controls;
using Infrastructure.ViewModels;

namespace UI.Controls.Dictionaries
{
    /// <summary>
    /// Interaction logic for PublisherGrid.xaml
    /// </summary>
    public partial class PublisherGrid : UserControl, IDictionaryGrid
    {
        AllCompaniesViewModel _allCompanies;

        /// <summary>
        /// Selection changed event
        /// </summary>
        public SelectionChangedEventHandler SelectionChanged;

        /// <summary>
        /// Called when [selection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        protected void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(SelectionChanged != null)
            {
                SelectionChanged(sender, e);
            }
        }

        /// <summary>
        /// Gets or sets the grid items.
        /// </summary>
        /// <value>The items.</value>
        public IEnumerable Items
        {
            get
            {
                return DataContext as IEnumerable;
            }
            set
            {
                DataContext = value;
                Publishers.ItemsSource = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PublisherGrid"/> class.
        /// </summary>
        public PublisherGrid()
        {
            InitializeComponent();

            
            LoadPublishers();
        }

        /// <summary>
        /// Loads the publishers into the grid.
        /// </summary>
        void LoadPublishers()
        {
            _allCompanies = new AllCompaniesViewModel();
            Items = _allCompanies.Companies;
            _allCompanies.Loaded += delegate 
                    {
                        SetSelectedItem();
                    };
        }

        CompanyViewModel _selected;
        /// <summary>
        /// Gets or sets the selected item.
        /// </summary>
        /// <value>The selected item.</value>
        public object Selected
        {
            get
            {
                return _selected;
            }
            set
            {
                _selected = value as CompanyViewModel;
                SetSelectedItem();
            }
        }

        /// <summary>
        /// Set the selected item of the grid based on database identity
        /// </summary>
        void SetSelectedItem()
        {
            if (Selected != null)
            {
                foreach (CompanyViewModel publisher in Publishers.Items)
                {

                    if (publisher.Id == _selected.Id)
                    {
                        Publishers.SelectedItem = publisher;
                    }
                }
            }
        }

        /// <summary>
        /// Updates the grid after edit operation.
        /// </summary>
        /// <param name="operation">The operation.</param>
        /// <param name="item">The item.</param>
        public void UpdateGrid(UpdateOperation operation, object item)
        {
            LoadPublishers();
        }

        /// <summary>
        /// Handles the SelectionChanged event of the Publishers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void Publishers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _selected = Publishers.SelectedItem as CompanyViewModel;
            OnSelectionChanged(sender, e);
        }
    }
}
